//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
		QrCodeUrl: '',
		curLocation: {},
		chooseAddress: {},
		latitude: '',
		longitude: ''

    },
    //事件处理函数
    bindViewTap: function() {
		wx.scanCode({
			success: (res) => {
				console.log('or', res)
			}
		})
    },

	openMap: function (e) {
		console.log('openMap', e)
		wx.getLocation({
			type: 'gcj02', //返回可以用于wx.openLocation的经纬度
			success: function(res) {
				var latitude = res.latitude
				var longitude = res.longitude
				wx.openLocation({
					latitude: latitude,
					longitude: longitude,
					scale: 28
				})

				console.log('openMap', res)
			}
		})
	},
	showToast: function () {
		wx.showToast({
			title: '成功',
			icon: 'success',
			duration: 2000
		})
	},

    onLoad: function () {
		var that = this;
		var window = window;

		wx.request({
            url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx8e01745268505dcc&secret=7eae1ac6c60ea22c7c675436c6de6dab',
            success: function (res) {
            	console.log(res)
				var access_token = res.data.access_token
				wx.request({
					url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' + access_token,
					method: 'POST',
					responseType: 'arraybuffer',
					header: {
						'content-type': 'application/json;charset=utf-8'
				   },
					data: {
						"path": "pages/index/index", "width": 430
					},
					success: function (res) {
                    
					var base64 = wx.arrayBufferToBase64(res.data);
					
					console.log({resdata: res.data, base64});

                    that.setData({ QrCodeUrl:"data:image/PNG;base64,"+ base64})
					}
				})
            }
		})
		// wx.chooseAddress({
		// 	success: function (res) {
		// 		that.setData({
		// 			chooseAddress: res
		// 		})
		// 		console.log('chooseAddress', res)
		// 		console.log(res.userName)
		// 		console.log(res.postalCode)
		// 		console.log(res.provinceName)
		// 		console.log(res.cityName)
		// 		console.log(res.countyName)
		// 		console.log(res.detailInfo)
		// 		console.log(res.nationalCode)
		// 		console.log(res.telNumber)
		// 	}
		// })
    },
	onHide: function() {
		console.log('onHide')
    },

	onLaunch: function () {
        console.log('onlaunch')
	},
	onShow: function() {
		var that = this;
		// wx.getLocation({
		// 	type: 'gcj02', //返回可以用于wx.openLocation的经纬度
		// 	success: function(res) {
		// 		console.log('get map', res)
		// 		that.setData({
		// 			latitude : res.latitude,
		// 			longitude : res.longitude
		// 		})
		// 	}
		// })
		// wx.showNavigationBarLoading();
		wx.setNavigationBarTitle({
			title: 'Profile'
		});
		// var that =this;
		// wx.getLocation({
		// 	success: function (res) {
		// 		wx.request({
		// 			url: 'http://api.map.baidu.com/geocoder/v2/?ak=fjke3YUipM9N64GdOIh1DNeK2APO2WcT&location=' + res.latitude + ',' + res.longitude + '&output=json&pois=1',
		// 			data: {},
		// 			header: { 'Content-Type': 'application/xml'},
		// 			dataType:'json',
		// 			success: function(ops) {
		// 				console.log('getLocation',ops.data.result);
		// 				that.setData({
		// 					curLocation: ops.data.result
		// 				})
		// 			}
		// 		})
		
		// 	},fail:function(){
		// 		console.log("fail");
		// 	}
		// })

	},

	onUnlaunch: function() {
		console.log('onUnlaunch')
    },

	userInfoReadyCallback: function() {
		console.log('userInfoReadyCallback')
    },

    getUserInfo: function(e) {
		console.log({e})
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    }
})
